﻿using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace SilverlightTetris_M10CDE.Web
{
    /// <summary>
    /// Defines methods to encrypt/decrypt a string
    /// </summary>
    public class Encryption
    {
        private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");

        /// <summary>
        /// Encrypts a string 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="sharedSecret"></param>
        /// <returns></returns>
        public static String EncryptString(string text, string sharedSecret)
        {
            if (text == null || sharedSecret == null)
                throw new ArgumentNullException("Please fill both fields"); // exception handling 

            string cipher = null;   // output text 
            RijndaelManaged Ecobj = null;  //  object used to encrypt data

            try
            {
                Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

                Ecobj = new RijndaelManaged();
                Ecobj.Key = key.GetBytes(Ecobj.KeySize / 8);
                Ecobj.IV = key.GetBytes(Ecobj.BlockSize / 8);

                ICryptoTransform encryptor = Ecobj.CreateEncryptor(Ecobj.Key, Ecobj.IV);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs))
                        {

                            //Write all data to the stream.
                            sw.Write(text);
                        }
                    }
                    cipher = Convert.ToBase64String(ms.ToArray());
                }

            }
            finally
            {
                if (Ecobj != null)
                    Ecobj.Clear();
            }
            return cipher;
        }

        /// <summary>
        /// Descrypts a string
        /// </summary>
        /// <param name="cipher"></param>
        /// <param name="sharedSecret"></param>
        /// <returns></returns>
        public static String Decrypt(string cipher, string sharedSecret)
        {
            if (cipher == null || sharedSecret == null)
                throw new ArgumentNullException("Please fill all fields");

            string plaintext = null;
            RijndaelManaged Ecobj = null;

            try
            {
                Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

                Ecobj = new RijndaelManaged();
                Ecobj.Key = key.GetBytes(Ecobj.KeySize / 8);
                Ecobj.IV = key.GetBytes(Ecobj.BlockSize / 8);

                ICryptoTransform decryptor = Ecobj.CreateDecryptor(Ecobj.Key, Ecobj.IV);

                byte[] bytes = Convert.FromBase64String(cipher);
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader sr = new StreamReader(cs))
                            plaintext = sr.ReadToEnd();
                    }
                }
            }

            finally
            {
                if (Ecobj != null)
                    Ecobj.Clear();
            }
            return plaintext;
        }
    }
}